<?php
defined('IN_YuLin') || exit('NO PERMIT!');

$mainTab   = Table('lottery_play_main');
$typeTab   = Table('lottery_play_type');
$followTab = Table('lottery_play_follow');
$cateTab   = Table('lottery_category');
$tmpPage   = '';
$operation = NoBadStr($_REQUEST['operation']);

// 获取彩种类别信息，并IdKey
$sql       = 'SELECT id, name FROM ' . $cateTab;
$cateList  = $db->getall($sql);
$cateIdKey = array();
foreach ($cateList as $k => $v){
    $cateIdKey[$v['id']] = $v;
}
$sql       = 'SELECT id, title, en_code FROM ' . $typeTab;
$typeList  = $db->getall($sql);
$typeIdKey = array();
foreach ($typeList as $tk => $tv){
    $typeIdKey[$tv['id']] = $tv;
}
$sql       = 'SELECT id, title, en_code FROM ' . $mainTab;
$mainList  = $db->getall($sql);
$mainIdKey = array();
foreach ($mainList as $mk => $mv){
    $mainIdKey[$mv['id']] = $mv;
}
switch ($operation){
    case 'playmain_list':
        $cate_id = NoBadStr($_GET['cate_id']);
        $data    = [];
        $where   = ' FROM '.$mainTab.' t WHERE 1 ';
        
        if ($cate_id) $where .= ' AND cate_id = ' . $cate_id;
        
        $tmp          = $db->getrow('SELECT COUNT(1) AS p '.$where);
        if($tmp['p']) {
            $page     = max(1,intval($_GET['page']));
            $size     = 20;
            $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
            $url      = '?m='.$m.'&c='.$c.'&title='.$title.'&cid='.$cid;
            $orderby  = ' ORDER BY sort ASC ';
            $data     = $db->getall('SELECT t.* '.$where.$orderby.$limit);
            $multi    = Multi($url,$page,$tmp['p'],$size);
        }
        $tmpPage      = 'lottery/playmain_list';
        break;
    case 'playmain_view':
        $id      = NoBadStr($_REQUEST['id']);
        if($id){
            $sql     = 'SELECT id, title, en_code, cate_id, sort, status FROM ' . $mainTab . ' WHERE id = ' . $id;
            $info    = $db->getrow($sql);
        }
        $tmpPage = 'lottery/playmain_view';
        break;
    case 'playmain_del':
        $id      = NoBadStr($_REQUEST['id']);
        $id && $db->exec('DELETE FROM ' . $mainTab . ' WHERE id = ' . $id);
        ShowMsg('删除成功',U($m.'/'.$c));
        break;
    case 'playmain_add':
        $id      = NoBadStr($_POST['id']);
        $ins     = array(
            'title'     => NoBadStr($_REQUEST['title']),
            'en_code'   => NoBadStr($_REQUEST['en_code']),
            'sort'      => NoBadStr($_REQUEST['sort']),
            'cate_id'   => NoBadStr($_REQUEST['cate_id']),
            'status'    => NoBadStr($_REQUEST['status'])
        );
        if ($id){
            // 修改操作
            $execSql = 'UPDATE ' . $mainTab . ' SET ' . CreateUpdateSql($ins) . ' WHERE id = ' . $id;
        }else{
            // 新增
            $execSql = 'INSERT INTO ' . $mainTab . ' SET ' . CreateUpdateSql($ins);
        }
        $flag   = $db->exec($execSql);
        if ($flag){
            ShowMsg('操作成功', U('lottery/play', array('operation' => 'playmain_list')));
        }else{
            ShowMsg('操作失败', -1);
        }
        break;
    case 'playtype_list':
        $data  = [];
        $where = ' FROM ' . $typeTab . ' t WHERE 1 ';
        
        $cid   = intval($_GET['cid']);
        $cid && $where .= ' AND cid = '.$cid;
        
        $tmp          = $db->getrow('SELECT COUNT(1) AS p '.$where);
        if($tmp['p']) {
            $page     = max(1,intval($_GET['page']));
            $size     = 20;
            $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
            $url      = '?m='.$m.'&c='.$c.'&cid='.$cid;
            $orderby  = ' ORDER BY sort ASC ';
            $data     = $db->getall('SELECT t.* '.$where.$orderby.$limit);
            $multi    = Multi($url,$page,$tmp['p'],$size);
        }
        $tmpPage      = 'lottery/playtype_list';
        break;
    case 'playtype_view':
        $id      = NoBadStr($_REQUEST['id']);
        if($id){
            $sql     = 'SELECT id, title, en_code, sort FROM ' . $typeTab . ' WHERE id = ' . $id;
            $info    = $db->getrow($sql);
        }
        $tmpPage = 'lottery/playtype_view';
        break;
    case 'playtype_add':
        $id      = NoBadStr($_POST['id']);
        $ins     = array(
            'title'     => NoBadStr($_REQUEST['title']),
            'en_code'   => NoBadStr($_REQUEST['en_code']),
            'sort'      => NoBadStr($_REQUEST['sort']),
        );
        if ($id){
            // 修改操作
            $execSql = 'UPDATE ' . $typeTab . ' SET ' . CreateUpdateSql($ins) . ' WHERE id = ' . $id;
        }else{
            // 新增
            $execSql = 'INSERT INTO ' . $typeTab . ' SET ' . CreateUpdateSql($ins);
        }
        $flag   = $db->exec($execSql);
        if ($flag){
            ShowMsg('操作成功', U('lottery/play', array('operation' => 'playtype_list')));
        }else{
            ShowMsg('操作失败', -1);
        }
        break;
    case 'playtype_del':
        $id      = NoBadStr($_REQUEST['id']);
        $id && $db->exec('DELETE FROM ' . $typeTab . ' WHERE id = ' . $id);
        ShowMsg('删除成功', U('lottery/play', array('operation' => 'playtype_list')));
        break;
    case 'playfollow_list':
        $cate_id = NoBadStr($_GET['cate_id']);
        $main_id = NoBadStr($_GET['main_id']);
        $data  = [];
        $where = ' FROM '.$followTab.' t WHERE 1 ';
        if ($cate_id) $where .= ' AND cate_id = ' . $cate_id;
        if ($main_id) $where .= ' AND main_id = ' . $main_id;
        $tmp          = $db->getrow('SELECT COUNT(1) AS p '.$where);
        if($tmp['p']) {
            $page     = max(1,intval($_GET['page']));
            $size     = 20;
            $limit    = ' LIMIT '.($page - 1) * $size.','.$size;
            $url      = '?m='.$m.'&c='.$c.'&title='.$title.'&cid='.$cid;
            $orderby  = ' ORDER BY sort ASC ';
            $data     = $db->getall('SELECT t.* '.$where.$orderby.$limit);
            $multi    = Multi($url,$page,$tmp['p'],$size);
        }
        $tmpPage      = 'lottery/playfollow_list';
        break;
    case 'playfollow_view':
        $id      = NoBadStr($_GET['id']);
        $main_id = NoBadStr($_GET['main_id']);
        $cate_id = NoBadStr($_GET['cate_id']);
        if($id){
            $sql     = 'SELECT id, title, en_code, `status`, `explain`, sort, `example`, winner_money, cate_id, type_id FROM ' . $followTab . ' WHERE id = ' . $id;
            $info    = $db->getrow($sql);
        }
        $tmpPage = 'lottery/playfollow_view';
        break;
    case 'playfollow_del':
        $id      = NoBadStr($_REQUEST['id']);
        $id && $db->exec('DELETE FROM ' . $followTab . ' WHERE id = ' . $id);
        ShowMsg('删除成功', U('lottery/play', array('operation' => 'playfollow_list')));
        break;
    case 'playfollow_add':
        $id      = NoBadStr($_POST['id']);
        $ins     = array(
            'title'           => NoBadStr($_REQUEST['title']),
            'en_code'         => NoBadStr($_REQUEST['en_code']),
            'sort'            => NoBadStr($_REQUEST['sort']),
            'cate_id'         => NoBadStr($_REQUEST['cate_id']),
            'status'          => NoBadStr($_REQUEST['status']),
            'explain'         => NoBadStr($_REQUEST['explain']),
            'example'         => html_entity_decode(NoBadStr($_REQUEST['example'])),
            'winner_money'    => NoBadStr($_REQUEST['winner_money']),
            'cate_id'         => NoBadStr($_REQUEST['cate_id']),
            'type_id'         => NoBadStr($_REQUEST['type_id']),
            'main_id'         => NoBadStr($_REQUEST['main_id']),
        );
        if ($id){
            // 修改操作
            $execSql = 'UPDATE ' . $followTab . ' SET ' . CreateUpdateSql($ins) . ' WHERE id = ' . $id;
        }else{
            // 新增
            $execSql = 'INSERT INTO ' . $followTab . ' SET ' . CreateInsertSql($ins);
        }
        $flag   = $db->exec($execSql);
        if ($flag){
            ShowMsg('操作成功', U('lottery/play', array('operation' => 'playfollow_list', 'cate_id' => $ins['cate_id'], 'main_id' => $ins['main_id'])));
        }else{
            ShowMsg('操作失败', -1);
        }
        break;
    case 'setPlayItem':
        $follow_id  = NoBadStr($_GET['id']);
        $followInfo = $db->getrow('SELECT id, title, main_id, type_id, cate_id FROM ' . $followTab . ' WHERE id = ' . $follow_id);
        // 设置玩法选项界面
        $tmpPage = 'lottery/set_play_item';
        break;
    case 'savePlayItem':
        $param = json_decode($_POST['param'], true);
        print_r($param);
        exit;
        break;
    default:
        // 载入彩种，方便设置玩法等信息
        $tmpPage = 'lottery/play_list';
        break;
}





$tpl->display($tmpPage);